package ex0.expressions.operations;

import ex0.expressions.IExpression;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 10/18/13
 */
public abstract class BinaryOperation implements IExpression {

    private Double firstVal;
    private Double secondVal;
    private String firstStr;
    private String secondStr;

    public BinaryOperation(Double firstVal, Double secondVal, String firstStr, String secondStr) {
        this.firstVal = firstVal;
        this.secondVal = secondVal;
        this.firstStr = firstStr;
        this.secondStr = secondStr;
    }

    @Override
    public final Double evaluate() {
        return apply(firstVal, secondVal);
    }

    @Override
    public final String toStr() {
        return "(" + firstStr + getOperationType() + secondStr + ")";
    }

    /**
     * @return operation type as string, e.g. "+"
     */
    protected abstract String getOperationType();

    /**
     * Applies operation to given values
     * @param first first value
     * @param second second value
     * @return operation evaluation result
     */
    protected abstract Double apply(Double first, Double second);
}
